package cn.wp.system.controller;

import cn.wp.basic.util.AjaxResult;
import cn.wp.basic.util.PageList;
import cn.wp.system.domain.Detail;
import cn.wp.system.query.DetailQuery;
import cn.wp.system.service.IDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典详情接口
 */
@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    /**
     * 分页查询显示
     * @param detailQuery
     * @return
     */
    @ApiOperation(value = "分页查询显示")
    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery detailQuery){
        PageList<Detail> detailPageList = detailService.queryPage(detailQuery);
        return detailService.queryPage(detailQuery);
    }

    /**
     * 新增和修改
     * @param detail
     * @return
     */
    @ApiOperation(value = "数据字典详情添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        try {
            if (detail.getId() == null){
                detailService.add(detail);
            } else {
                detailService.update(detail);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据字典详情对象",notes = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult delect(@PathVariable("id") Long id){
        try {
            detailService.delete(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }
}
